package com.sailing.xjpb.common.converters;

import com.google.common.collect.Lists;

import java.lang.reflect.Array;
import java.util.List;

/**
 * 对象转list
 */
public final class ListConverter implements Converter {

    @Override
    public <T> T convert(Class<T> type, Object value) {
        if (value == null) {
            return null;
        }
        Class<?> valueClass = value.getClass();
        if (valueClass == type) {
            return type.cast(value);
        }
        List<Object> valueList = Lists.newArrayList();
        if (valueClass.isArray()) {
            int size = Array.getLength(value);
            for (int i = 0; i < size; i++) {
                Object v = Array.get(value, i);
                valueList.add(v);
            }
        } else {
            valueList.add(value);
        }
        return type.cast(valueList);
    }
}
